using System;
using System.Web;
using System.Reflection;

namespace S7.DALFactory
{
    /// <summary>
    /// [类 描 述]：数据缓存类
    /// [文 件 名]：S7.Factory.DataCache.cs
    /// [作    者]：徐永明 jxymnet@163.com
    /// [版    本]：1.0.0.0
    /// [创建日期]: 2012-09-10
    /// Copyright © 2012 s7kj.com 圣骑科技有限公司 版权所有。 
    /// </summary>
    public class DataCache
    {
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        #region CreateObject

        /// <summary>
        /// 不使用缓存
        /// </summary>
        /// <param name="AssemblyPath"></param>
        /// <param name="classNamespace"></param>
        /// <returns></returns>
        public static object CreateObjectNoCache(string AssemblyPath, string classNamespace, object[] args)
        {
            try
            {
                if (args == null)
                {
                    return Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                }
                else
                {
                    return Assembly.Load(AssemblyPath).CreateInstance(classNamespace, false, BindingFlags.CreateInstance, null, args, null, null);
                }
            }
            catch (Exception)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        public static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            return CreateObjectNoCache(AssemblyPath, classNamespace, null);
        }

        /// <summary>
        /// 使用缓存
        /// </summary>
        /// <param name="AssemblyPath"></param>
        /// <param name="classNamespace"></param>
        /// <returns></returns>
        public static object CreateFSObject(string AssemblyPath, string classNamespace, Type t, object[] args)
        {
            object objType = DataCache.GetCache(classNamespace);

            if (objType == null)
            {
                try
                {
                    Assembly assembly = Assembly.Load(AssemblyPath);
                    classNamespace += "[[" + t.FullName + "," + t.Assembly.FullName + "]]";

                    if (args == null)
                    {
                        objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    }
                    else
                    {
                        objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace, false, BindingFlags.CreateInstance, null, args, null, null);
                    }

                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch (Exception)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }
        public static object CreateFSObject(string AssemblyPath, string classNamespace, Type t)
        {
            return CreateFSObject(AssemblyPath, classNamespace, t, null);
        }
        public static object CreateObject(string AssemblyPath, string classNamespace, object[] args)
        {
            object objType = DataCache.GetCache(classNamespace);

            if (objType == null)
            {
                try
                {
                    Assembly assembly = Assembly.Load(AssemblyPath);

                    if (args == null)
                    {
                        objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    }
                    else
                    {
                        objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace, false, BindingFlags.CreateInstance, null, args, null, null);
                    }
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch (Exception) { }
            }
            return objType;
        }
        public static object CreateObject(string AssemblyPath, string classNamespace)
        {
            return CreateObject(AssemblyPath, classNamespace, null);
        }
        #endregion
    }
}
